package com.xqboss.apps.listener;

import com.xqboss.apps.enums.mq.AppMqDelayEnum;
import com.xqboss.apps.service.market.MarketService;
import com.xqboss.common.core.mq.MqEnum;
import com.xqboss.common.core.mq.MqListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.apis.consumer.ConsumeResult;
import org.apache.rocketmq.client.apis.message.MessageView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.nio.charset.StandardCharsets;

/**
 * 关闭寄售自动下架市场专卖中消息
 *
 * @author Administrator
 */
@Component
@Slf4j
public class MarketOffShelfListener extends MqListener {

    @Autowired
    private MarketService marketService;

    @Override
    public MqEnum getEnum() {
        return AppMqDelayEnum.MARKET_AUTO_OFF_SHELF;
    }

    @Override
    @Transactional
    public ConsumeResult solve(MessageView message) {
        String value = StandardCharsets.UTF_8.decode(message.getBody()).toString();
        log.info("关闭寄售自动下架市场专卖中消费 value:{}", value);
        String[] split = value.split(",");
        //藏品，装备，宝盒id
        long sourceId = Long.parseLong(split[0].replace("\"", ""));
        Integer type = Integer.valueOf(split[1].replace("\"", ""));

        marketService.offShelfAuto(sourceId, type);
        //删除市场表已售出的藏品,防止老数据影响最高价格
        marketService.delMarketByNftId(sourceId, type);
        return ConsumeResult.SUCCESS;
    }
}
